package com.rzico.quartz.job;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.common.utils.DateUtil;
import com.rzico.admin.entity.Project;
import com.rzico.admin.entity.ProjectActivity;
import com.rzico.admin.entity.ProjectExecute;
import com.rzico.admin.service.ProjectActivityService;
import com.rzico.admin.service.ProjectExecuteService;
import com.rzico.admin.service.ProjectService;
import com.rzico.base.CommResult;
import com.rzico.core.entity.*;
import com.rzico.core.plugin.MsgPlugin;
import com.rzico.core.service.SysJobService;
import com.rzico.core.service.SysPluginService;
import com.rzico.core.service.SysUserService;
import com.rzico.util.DateUtils;
import com.rzico.util.Keys;
import com.rzico.util.RedisHandler;
import com.rzico.util.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zhongzm on 2020/2/2.
 * 计划任务执行demo
 */
public class AffairAlert implements Job {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected SysJobService sysJobService;

    @Autowired
    protected SysPluginService sysPluginService;

    @Autowired
    protected SysUserService sysUserService;

    @Autowired
    protected ProjectService projectService;

    @Autowired
    protected ProjectExecuteService projectExecuteService;

    @Autowired
    protected ProjectActivityService projectActivityService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String jobId = jobExecutionContext.getTrigger().getKey().getName();
        SysJob sysJob = sysJobService.selectByPrimaryKey(jobId);
        try {

            if (sysJob.getExecStatus().equals(1)) {
                //不能重复执行
                return;
            }

            sysJob.setExecStatus(1);
            sysJob.setJobDesc("开始执行:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);

            // 这里开始查询5分钟时查一次,1.处理已完成本期周期团购 2.对

            Map<String,Object> params = new HashMap<>();
            params.put("status",2);

            SysPlugin sysPlugin = sysPluginService.getEnabledPlugin("10200",SysPlugin.MSGPLUGIN);

            if (sysPlugin==null) {
                return;
            }

            List<Project> projectList = projectService.selectList(params);

            for (Project proj:projectList) {
                try {

                    Map<String,Object> wparams = new HashMap<>();
                    wparams.put("projectId",proj.getId());
                    wparams.put("startDate", org.apache.commons.lang3.time.DateUtils.truncate(new Date(),Calendar.DATE));
                    wparams.put("endDate",org.apache.commons.lang3.time.DateUtils.addDays(org.apache.commons.lang3.time.DateUtils.truncate(new Date(),Calendar.DATE),1));
                    Integer w1 = projectExecuteService.selectRowCount(wparams);
                    Integer w2 = projectActivityService.selectRowCount(wparams);

                    if (w1>0 && w2>0) {
                        continue;
                    }

                    SysUser sysUser = sysUserService.selectByPrimaryKey(proj.getUserId());

                    String msg = "【龙山双创】您项目"+proj.getProjectName()+"的月度活动报表没有上传，请尽快处理";
                    try {
                        MsgPlugin msgPlugin = sysPluginService.getMsgPlugin(sysPlugin.getPluginId());
                        msgPlugin.sendMsg(sysPlugin,sysUser.getUsername(),msg);
                    } catch (Exception e) {

                    }

                } catch (Exception e) {
                    logger.error(e.getMessage());
                }

            }

            // end

            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行成功:"+sdf.format(new Date())+",下次执行:"+sdf.format(jobExecutionContext.getNextFireTime()));
            sysJobService.updateByPrimaryKeySelective(sysJob);

        }
        catch (Exception e) {

            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行错误:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);

        }

    }
}
